package com.qckj.amz.controller;

import com.qckj.amz.domain.ProductTempDesc;
import com.qckj.amz.service.IProductTempDescService;
import com.qckj.common.annotation.Log;
import com.qckj.common.core.controller.BaseController;
import com.qckj.common.core.domain.AjaxResult;
import com.qckj.common.core.page.TableDataInfo;
import com.qckj.common.enums.BusinessType;
import com.qckj.common.utils.StringUtils;
import com.qckj.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 产品描述信息Controller
 *
 * @author luchuan
 * @since 1.0 2021-12-28
 */
@RestController
@RequestMapping("/amz/productTempDesc")
public class ProductTempDescController extends BaseController {
    @Autowired
    private IProductTempDescService productTempDescService;

    /**
     * 查询产品描述信息列表
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempDesc:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductTempDesc productTempDesc) {
        Page<ProductTempDesc> page = productTempDescService.findProductTempDescPage(productTempDesc);
        return getDataTable(page);
    }

    /**
     * 导出产品描述信息列表
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempDesc:export')")
    @Log(title = "产品描述信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProductTempDesc productTempDesc) {
        List<ProductTempDesc> list = productTempDescService.findProductTempDescList(productTempDesc);
        ExcelUtil<ProductTempDesc> util = new ExcelUtil<>(ProductTempDesc.class);
        return util.exportExcel(list, "productTempDesc");
    }

    /**
     * 获取产品描述信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempDesc:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(productTempDescService.findById(id));
    }

    /**
     * 新增产品描述信息
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempDesc:add')")
    @Log(title = "产品描述信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductTempDesc productTempDesc) {
        productTempDescService.save(productTempDesc);
        return AjaxResult.success();
    }

    /**
     * 修改产品描述信息
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempDesc:edit')")
    @Log(title = "产品描述信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductTempDesc productTempDesc) {
        productTempDescService.save(productTempDesc);
        return AjaxResult.success();
    }

    /**
     * 删除产品描述信息
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempDesc:remove')")
    @Log(title = "产品描述信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        productTempDescService.deleteByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

    /**
     * 通过产品代码获取产品描述信息
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempDesc:query')")
    @RequestMapping(value = "/getListByProductCode")
    public AjaxResult getListByProductCode(@RequestBody String productCode) {
        AjaxResult res = productTempDescService.getListByProductCode(productCode);
        return res;
    }

    /**
     * 新增或修改产品描述信息
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempDesc:add')")
    @PostMapping("/addOrUpdateTempDesc")
    public AjaxResult addOrUpdateTempDesc(@RequestBody Map<String, Object> descData) {
        AjaxResult res = productTempDescService.addOrUpdateTempDesc(descData);
        return res;
    }

    /**
     * 侵权词替换
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempDesc:infringingWordReplace')")
    @Log(title = "侵权词替换", businessType = BusinessType.INFRINGINGWORDREPLACE)
    @GetMapping("/infringingWordReplace/{productCodes}")
    public AjaxResult infringingWordReplace(@PathVariable List<String> productCodes) {
        if (productCodes==null || productCodes.isEmpty()){
            return AjaxResult.error("产品代码不能为空！");
        }
        AjaxResult res = productTempDescService.infringingWordReplace(productCodes);
        return res;
    }

    /**
     * 产品翻译
     * @param productCodes          产品代码集合
     * @param translationToolType   翻译工具类型
     * @param countryFrom           源站点
     * @param countryCodes          译站点集合
     * @return
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempDesc:translate')")
    @Log(title = "产品翻译", businessType = BusinessType.TRANSLATE)
    @PostMapping("/translate")
    public AjaxResult translate(@RequestParam(name = "productCodes") List<String> productCodes,
                                @RequestParam(name = "translationToolType") String translationToolType,
                                @RequestParam(name = "countryFrom") String countryFrom,
                                @RequestParam(name = "countryCodes") List<String> countryCodes) {

        if (productCodes==null || productCodes.isEmpty()){
            return AjaxResult.error("产品代码不能为空！");
        }else if (StringUtils.isBlank(countryFrom)){
            return AjaxResult.error("源语言不能为空！");
        }else if (countryCodes.size()==0){
            return AjaxResult.error("目标语言至少选择一个！");
        }

        AjaxResult res = productTempDescService.translate(productCodes,translationToolType,countryFrom,countryCodes);
        return res;
    }

    /**
     * 清空翻译（清空项不包含中文和英文）
     */
    @PreAuthorize("@ss.hasPermi('amz:productTempDesc:clearTranslate')")
    @Log(title = "清空翻译（清空项不包含中文和英文）", businessType = BusinessType.CLEARTRANSLATE)
    @GetMapping("/clearTranslate/{productCodes}")
    public AjaxResult clearTranslate(@PathVariable List<String> productCodes) {
        if (productCodes==null || productCodes.isEmpty()){
            return AjaxResult.error("产品代码不能为空！");
        }
        AjaxResult res = productTempDescService.clearTranslate(productCodes);
        return res;
    }


}
